package biz;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import java.util.List;

import dao.TOutMain;
import dao.TOutDetail;

public class OutprodBiz {
	Configuration configuration = null;
	SessionFactory sessionFactory = null;
	Session session = null;
	Transaction transaction = null;
	
	public void AddOutMain(TOutMain sarv){
		ModOutMain(sarv);
	}
	
	public void ModOutMain(TOutMain sarv){
		String maxid = "";
		String qtype = "X";
		DbComBiz dbbiz = new DbComBiz();
		if(null == sarv.getId() || "".equals(sarv.getId().trim())){
			maxid = dbbiz.getMaxId(qtype);
			sarv.setId(qtype+maxid);
		}
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		session.saveOrUpdate(sarv);
		transaction.commit();
		session.close();
	}
	
	public void DelOutMain(TOutMain sarv){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		TOutMain tddmp = (TOutMain)session.get(TOutMain.class, sarv.getId());
		if(tddmp != null) {
			session.delete(tddmp);
		}
		transaction.commit();
		session.close();
	}
	
	public List GetOutMain(String orderid){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("from TOutMain as f where f.jeworderid='" + orderid + "' order by f.id");
		
		List list =query.list();
		session.close();
		return list;
	}
	
	public void AddOutDetail(TOutDetail sarv){
		ModOutDetail(sarv);
	}
	
	public void ModOutDetail(TOutDetail sarv){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
		session.saveOrUpdate(sarv);
		transaction.commit();
		session.close();
	}
	
	public void DelOutDetail(TOutDetail sarv){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		TOutDetail tddmp = (TOutDetail)session.get(TOutDetail.class, sarv.getId());
		transaction = session.beginTransaction();
		if(tddmp != null) {
			session.delete(tddmp);
		}
		transaction.commit();
		session.close();
	}
	
	public List GetOutDetail(String outid){
		configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		session = sessionFactory.openSession();
		
		Query query = session.createQuery("from TOutDetail as f where f.jeworderid='" + outid + "' order by f.id");
		
		List list =query.list();
		session.close();
		return list;
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
